package com.elegantsoft.pakistannews.rssfeed;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.actionbarsherlock.view.MenuItem;

import org.xmlpull.v1.XmlSerializer;

import com.actionbarsherlock.app.SherlockListActivity;
import com.elegantsoft.pakistannews.AlternateSend;
import com.elegantsoft.pakistannews.Channel;
import com.elegantsoft.pakistannews.CutomBaseAdapter;
import com.atiffarrukh.pakistannews.R;
import com.elegantsoft.pakistannews.WebViewT;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.google.analytics.tracking.android.EasyTracker;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class MessageList extends SherlockListActivity {
	// Progress Dialog
	public ProgressDialog pDialog;
	ArrayList<HashMap<String, String>> menuItems;
	private List<Message> messages;
	String url = null;
	public static String TAG_TITLE;
	public static String TAG_DESCRIPTION;
	public static String TAG_IMAGE;
	public static String TAG_DATE;
	
	private boolean unknown;
	String cat, channel, a, b;
	String[] urlArray;
	boolean alternate,english,anyone;
	SharedPreferences prefs;
	int counter;
	
	

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		// Look up the AdView as a resource and load a request.
	    AdView adView = (AdView)this.findViewById(R.id.adView);
	    adView.loadAd(new AdRequest());
		
	   
		
		
		
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		TAG_TITLE = "title";
		TAG_DESCRIPTION = "description";
		TAG_DATE = "pubDate";
		TAG_IMAGE = "img";
		anyone = false;
		
		prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		
		Bundle extras = getIntent().getExtras();
		//english = extras.getBoolean("english");
		cat = extras.getString("category");
		channel = extras.getString("channelName");
		alternate = extras.getBoolean("alternate");
		english = extras.getBoolean("english");
		
		unknown = false;
		url = urlDeterminer();
		new LoadFeed().execute();
		// loadFeed(ParserType.XML_PULL);
		// updateListUI();
	}

	private String[] urlArrayGetter() {
		// TODO Auto-generated method stub
		//urlArray = new String[] {"http://feeds.feedburner.com/geo/dTwR","http://news.ptv.com.pk/rss/news.xml"};
		
		counter = 0;
		urlArray =  new String[10];
		
		if(cat.equals("Headlines")){
			if (prefs.getBoolean("Geo",false)) {
				Log.d("before giving Geo link", "True");
				urlArray[counter] = "http://feeds.feedburner.com/geo/dTwR";
				Log.d("giving Geo link", "True");
				anyone = true;
				counter++;
			} if (prefs.getBoolean("Dawn",false)) {
				urlArray[counter] = "http://feeds2.feedburner.com/dawn-all-news";
				anyone = true;
				counter++;
			} if (prefs.getBoolean("Google News",false)) {
				urlArray[counter] = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&output=rss";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Express Tribune",false)) {
				urlArray[counter] = "http://tribune.com.pk/pakistan/feed/";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("PTV News",false)) {
				urlArray[counter] = "http://news.ptv.com.pk/rss/news.xml";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Aaj News",false)) {
				urlArray[counter] = "http://www.aaj.tv/feed/";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("The News",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/com/cwEr";
				counter++;
				anyone = true;
			}
			Log.d("returning", "True");
			return urlArray;
		} else if(cat.equals("Pakistan")){
			if (prefs.getBoolean("GeoP",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/geo/GiKR?format=xml";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("DawnP",false)) {
				urlArray[counter] = "http://feeds2.feedburner.com/dawn-all-news";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Google NewsP",false)) {
				urlArray[counter] = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&output=rss";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Express TribuneP",false)) {
				urlArray[counter] = "http://tribune.com.pk/pakistan/feed/";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("PTV NewsP",false)) {
				urlArray[counter] = "http://news.ptv.com.pk/rss/news.xml";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Aaj NewsP",false)) {
				urlArray[counter] = "http://www.aaj.tv/feed/";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("The NewsP",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/TheNewsInternational-National";
				counter++;
				anyone = true;
			}
			return urlArray;
		}  else if(cat.equals("World")){
			if (prefs.getBoolean("GeoW",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/geo/wUIl";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Express TribuneW",false)) {
				urlArray[counter] = "http://tribune.com.pk/world/feed/";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("The NewsW",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/World-TheNewsInternational";
				counter++;
				anyone = true;
			}
			return urlArray;
		} else if(cat.equals("Buisness")){
			if (prefs.getBoolean("GeoB",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/geo/hegG";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Express TribuneB",false)) {
				urlArray[counter] = "http://tribune.com.pk/business/feed/";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Buisness RecorderB",false)) {
				urlArray[counter] = "http://www.brecorder.com/rss/?feed_id=2&format=raw";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("The NewsB",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/TheNewsInternational-Business";
				counter++;
				anyone = true;
			}
			return urlArray;
		} else if(cat.equals("Sports")){
			if (prefs.getBoolean("GeoS",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/GeoSport-GeoTvNetwork";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Express TribuneS",false)) {
				urlArray[counter] = "http://tribune.com.pk/sports/feed/";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("ESPN Pakistan CricketS",false)) {
				urlArray[counter] = "http://www.espncricinfo.com/rss/content/story/feeds/7.xml";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("The NewsS",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/TheNewsInternational-Sports";
				counter++;
				anyone = true;
			}
			return urlArray;
		} else if(cat.equals("Entertainment")){
			if (prefs.getBoolean("GeoE",false)) {
				urlArray[counter] = "http://feeds.feedburner.com/GeoEntertainment-GeoTvNetwork";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Express TribuneE",false)) {
				urlArray[counter] = "http://tribune.com.pk/life-style/feed/";
				counter++;
				anyone = true;
			} 
			return urlArray;
		} else if(cat.equals("Science")){
			if (prefs.getBoolean("Popular ScienceSC",false)) {
				urlArray[counter] = "http://feeds.popsci.com/c/34567/f/632422/index.rss";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("BBCSC",false)) {
				urlArray[counter] = "http://feeds.bbci.co.uk/news/science_and_environment/rss.xml";
				counter++;
				anyone = true;
			} 
			return urlArray;
		} else if(cat.equals("Technology")){
			if (prefs.getBoolean("Popular ScienceT",false)) {
				urlArray[counter] = "http://feeds.popsci.com/c/34567/f/632423/index.rss";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("BBCT",false)) {
				urlArray[counter] = "http://feeds.bbci.co.uk/news/technology/rss.xml";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("EngadgetT",false)) {
				urlArray[counter] = "http://www.engadget.com/rss.xml";
				counter++;
				anyone = true;
			} if (prefs.getBoolean("Pro PakistaniT",false)) {
				urlArray[counter] = "http://feeds2.feedburner.com/Propakistani";
				counter++;
				anyone = true;
			} 
			return urlArray;
		}
		return null;
	}

	private String urlDeterminer() {
		// TODO Auto-generated method stub
		
		// Toast.makeText(getApplicationContext(),"Category : " + cat +
		// " and Channel :" + channel, Toast.LENGTH_SHORT).show();
		if (!alternate && english) {
			/** if @Categories are selected and @English as Language*/
			if (cat.equals("Headlines")) {

				if (channel.equals("Geo")) {
					return url = "http://feeds.feedburner.com/geo/dTwR";
				} else if (channel.equals("Dawn")) {
					return url = "http://feeds2.feedburner.com/dawn-all-news";
				} else if (channel.equals("Google News")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&output=rss";
				} else if (channel.equals("Express Tribune")) {
					return url = "http://tribune.com.pk/pakistan/feed/";
				} else if (channel.equals("PTV News")) {
					return url = "http://news.ptv.com.pk/rss/news.xml";
				} else if (channel.equals("Aaj News")) {
					return url = "http://www.aaj.tv/feed/";
				} else if (channel.equals("The News")) {
					return url = "http://feeds.feedburner.com/com/cwEr";
				} else if (channel.equals("Selected Channels")) {
					Log.d("Selected Channels", "before" );
					urlArray = urlArrayGetter();
					Log.d("Selected Channels", "" + urlArray[0]);
					return null;
				}

			} else if (cat.equals("Pakistan")) {
				if (channel.equals("Geo")) {
					return "http://feeds.feedburner.com/geo/GiKR?format=xml";
				} else if (channel.equals("Dawn")) {
					return url = "http://feeds2.feedburner.com/dawn-all-news";
				} else if (channel.equals("Google News")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&topic=n&output=rss";
				} else if (channel.equals("Express Tribune")) {
					return url = "http://tribune.com.pk/pakistan/feed/";
				} else if (channel.equals("PTV News")) {
					return url = "http://news.ptv.com.pk/rss/news.xml";
				} else if (channel.equals("Aaj News")) {
					return url = "http://www.aaj.tv/feed/";
				} else if (channel.equals("The News")) {
					return url = "http://feeds.feedburner.com/TheNewsInternational-National";
				} else if (channel.equals("Selected Channels")) {
					urlArray = urlArrayGetter();
					return null;
				}
			} else if (cat.equals("World")) {
				if (channel.equals("Geo")) {
					return url = "http://feeds.feedburner.com/geo/wUIl";
				} else if (channel.equals("Google News")) {
					return url = "http://news.google.com/news?pz=1&cf=all&ned=en_pk&hl=en&topic=w&output=rss";
				} else if (channel.equals("Express Tribune")) {
					return url = "http://tribune.com.pk/world/feed/";
				} else if (channel.equals("The News")) {
					return url = "http://feeds.feedburner.com/World-TheNewsInternational";
				} else if (channel.equals("Selected Channels")) {
					urlArray = urlArrayGetter();
					return null;
				}
			} else if (cat.equals("Buisness")) {
				if (channel.equals("Geo")) {
					return url = "http://feeds.feedburner.com/geo/hegG";
				} else if (channel.equals("Google News")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&topic=b&output=rss";
				} else if (channel.equals("Express Tribune")) {
					return url = "http://tribune.com.pk/business/feed/";
				} else if (channel.equals("The News")) {
					return url = "http://feeds.feedburner.com/TheNewsInternational-Business";
				}  else if (channel.equals("Buisness Recorder")) {
					return url = "http://www.brecorder.com/rss/?feed_id=2&format=raw";
				} else if (channel.equals("Selected Channels")) {
					urlArray = urlArrayGetter();
					return null;
				} 
			} else if (cat.equals("Sports")) {
				if (channel.equals("Geo")) {
					return url = "http://feeds.feedburner.com/GeoSport-GeoTvNetwork";
				} else if (channel.equals("Google News")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&topic=s&output=rss";
				} else if (channel.equals("Express Tribune")) {
					return url = "http://tribune.com.pk/sports/feed/";
				} else if (channel.equals("The News")) {
					return url = "http://feeds.feedburner.com/TheNewsInternational-Sports";
				} else if (channel.equals("ESPN Pakistan Cricket")) {
					return url = "http://www.espncricinfo.com/rss/content/story/feeds/7.xml";
				} 
			} else if (cat.equals("Entertainment")) {
				if (channel.equals("Geo")) {
					return url = "http://feeds.feedburner.com/GeoEntertainment-GeoTvNetwork";
				} else if (channel.equals("Google News")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&topic=e&output=rss";
				} else if (channel.equals("Express Tribune")) {
					return url = "http://tribune.com.pk/life-style/feed/";
				} else if (channel.equals("Selected Channels")) {
					urlArray = urlArrayGetter();
					return null;
				}
			} else if (cat.equals("Health")) {

				if (channel.equals("Geo")) {
					return url = "http://feeds.feedburner.com/GeoHealth-GeoTvNetwork";
				} else if (channel.equals("Google News")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&topic=m&output=rss";
				}
			} else if (cat.equals("Science")) {
				if (channel.equals("Google News")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&topic=snc&output=rss";
				} else if (channel.equals("Popular Science")) {
					return url = "http://feeds.popsci.com/c/34567/f/632422/index.rss";
				} else if (channel.equals("BBC")) {
					return url = "http://feeds.bbci.co.uk/news/science_and_environment/rss.xml";
				} else if (channel.equals("Selected Channels")) {
					urlArray = urlArrayGetter();
					return null;
				}
			} else if (cat.equals("Technology")) {
				if (channel.equals("Google News")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&topic=tc&output=rss";
				} else if (channel.equals("Popular Science")) {
					return url = "http://feeds.popsci.com/c/34567/f/632423/index.rss";
				} else if (channel.equals("BBC")) {
					return url = "http://feeds.bbci.co.uk/news/technology/rss.xml";
				} else if (channel.equals("Engadged")) {
					return url = "http://www.engadget.com/rss.xml";
				} else if (channel.equals("Pro Pakistani")) {
					return url = "http://feeds2.feedburner.com/Propakistani";
				} else if (channel.equals("Selected Channels")) {
					urlArray = urlArrayGetter();
					return null;
				}
			}
		} else if(!alternate && !english){
			/** if @categories are selected and @Urdu as Language*/
			if (cat.equals("Headlines")) {
				if (channel.equals("Waqt")) {
					return url = "http://waqtnews.tv/feed";
				} else if (channel.equals("Nawai Waqt Urdu")) {
					return url = "http://www.nawaiwaqt.com.pk/rss_feeds";
				} else if (channel.equals("Jang News Urdu")) {
					return url = "http://mcs.geo.tv/jang/jang.xml";
				}
			} else if (cat.equals("Pakistan")) {
				if (channel.equals("Waqt")) {
					return url = "http://waqtnews.tv/feed";
				} else if (channel.equals("Nawai Waqt Urdu")) {
					return url = "http://www.nawaiwaqt.com.pk/rss_feeds";
				} else if (channel.equals("Jang News Urdu")) {
					return url = "http://jang.net/tp_db_agg/jangfeeds/National-News.xml";
				}
			} else if (cat.equals("World")) {
				if (channel.equals("Jang News Urdu")) {
					return url = "http://jang.net/tp_db_agg/jangfeeds/World-News.xml";
				}
			}else if (cat.equals("Buisness")) {
				if (channel.equals("Jang News Urdu")) {
					return url = "http://jang.net/tp_db_agg/jangfeeds/Business-News.xml";
				} 
			} else if (cat.equals("Sports")) {
				if (channel.equals("Jang News Urdu")) {
					return url = "http://jang.net/tp_db_agg/jangfeeds/Sports-News.xml";
				}
			} 
		} else if(alternate && english){
			/** if @Channels are selected and @English as Language*/
			if (channel.equals("Geo")) {

				if (cat.equals("Headlines")) {
					return url = "http://feeds.feedburner.com/geo/dTwR";
				} else if (cat.equals("Pakistan")) {
					return url = "http://feeds.feedburner.com/geo/GiKR";
				} else if (cat.equals("World")) {
					return url = "http://feeds.feedburner.com/geo/wUIl";
				} else if (cat.equals("Entertainment")) {
					return url = "http://feeds.feedburner.com/GeoEntertainment-GeoTvNetwork";
				} else if (cat.equals("Health")) {
					return url = "http://feeds.feedburner.com/GeoHealth-GeoTvNetwork";
				} else if (cat.equals("Buisness")) {
					return url = "http://feeds.feedburner.com/geo/hegG";
				}

			} else if (channel.equals("Dawn")) {

				if (cat.equals("Headlines")) {
					return url = "http://feeds2.feedburner.com/dawn-all-news";
				} else if (cat.equals("Pakistan")) {
					return url = "http://feeds2.feedburner.com/dawn-all-news";
				}

			} else if (channel.equals("Express Tribune")) {

				if (cat.equals("Headlines")) {
					return url = "http://tribune.com.pk/pakistan/feed/";
				} else if (cat.equals("Pakistan")) {
					return url = "http://tribune.com.pk/pakistan/feed/";
				} else if (cat.equals("World")) {
					return url = "http://tribune.com.pk/world/feed/";
				} else if (cat.equals("Buisness")) {
					return url = "http://tribune.com.pk/business/feed/";
				} else if (cat.equals("Entertainment")) {
					return url = "http://tribune.com.pk/life-style/feed/";
				} else if (cat.equals("Sports")) {
					return url = "http://tribune.com.pk/sports/feed/";
				}
			} else if (channel.equals("PTV News")) {
				if (cat.equals("Headlines")) {
					return url = "http://news.ptv.com.pk/rss/news.xml";
				} else if (cat.equals("Pakistan")) {
					return url = "http://news.ptv.com.pk/rss/news.xml";
				}
			} else if (channel.equals("Aaj News")) {
				if (cat.equals("Headlines")) {
					return url = "http://www.aaj.tv/feed/";
				} else if (cat.equals("Pakistan")) {
					return url = "http://www.aaj.tv/feed/";
				}
			} else if (channel.equals("The News")) {

				if (cat.equals("Headlines")) {
					return url = "http://feeds.feedburner.com/com/cwEr";
				} else if (cat.equals("Pakistan")) {
					return url = "http://feeds.feedburner.com/TheNewsInternational-National";
				} else if (cat.equals("World")) {
					return url = "http://feeds.feedburner.com/World-TheNewsInternational";
				} else if (cat.equals("Sports")) {
					return url = "ttp://feeds.feedburner.com/TheNewsInternational-Sports";
				} else if (cat.equals("Buisness")) {
					return url = "http://feeds.feedburner.com/TheNewsInternational-Business";
				}
			} else if (channel.equals("Popular Science")) {

				if (cat.equals("Technology")) {
					return url = "http://feeds.popsci.com/c/34567/f/632423/index.rss";
				} else if (cat.equals("Science")) {
					return url = "http://feeds.popsci.com/c/34567/f/632422/index.rss";
				}
			} else if (channel.equals("Pro Pakistani")) {

				if (cat.equals("Technology")) {
					return url = "http://feeds2.feedburner.com/Propakistani";
				}
			} else if (channel.equals("Buisness Recorder")) {

				if (cat.equals("Buisness")) {
					return url = "http://www.brecorder.com/rss/?feed_id=2&format=raw";
				}
			} else if (channel.equals("BBC")) {

				if (cat.equals("Technology")) {
					return url = "http://feeds.bbci.co.uk/news/technology/rss.xml";
				} else if (cat.equals("Science")) {
					return url = "http://feeds.bbci.co.uk/news/science_and_environment/rss.xml";
				}
			}

		} else if (alternate && !english){
			/** if @Channels are selected and @Urdu as Language*/
			if (channel.equals("Waqt")) {

				if (cat.equals("Headlines")) {
					return url = "http://waqtnews.tv/feed";
				} else if (cat.equals("Pakistan")) {
					return url = "http://waqtnews.tv/feed";
				}
			} else if (channel.equals("Nawai Waqt Urdu")) {
				if (cat.equals("Headlines")) {
					return url = "http://www.nawaiwaqt.com.pk/rss_feeds";
				} else if (cat.equals("Pakistan")) {
					return url = "http://www.nawaiwaqt.com.pk/rss_feeds";
				}
			} else if (channel.equals("Jang News Urdu")) {

				if (cat.equals("Headlines")) {
					return url = "http://news.google.com.pk/news?pz=1&cf=all&ned=en_pk&hl=en&topic=tc&output=rss";
				} else if (cat.equals("Pakistan")) {
					return url = "http://jang.net/tp_db_agg/jangfeeds/National-News.xml";
				} else if (cat.equals("World")) {
					return url = "http://jang.net/tp_db_agg/jangfeeds/World-News.xml";
				} else if (cat.equals("Buisness")) {
					return url = "http://jang.net/tp_db_agg/jangfeeds/Business-News.xml";
				} else if (cat.equals("Sports")) {
					return url = "http://jang.net/tp_db_agg/jangfeeds/Sports-News.xml";
				}
			} 
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		/*ParserType type = ParserType.values()[item.getItemId()];
		ArrayAdapter<String> adapter = (ArrayAdapter<String>) this
				.getListAdapter();
		if (adapter.getCount() > 0) {
			adapter.clear();
		}
		this.loadFeed(type);
		
		return true;*/
		Intent intentHome;
		switch (item.getItemId()) {
		
		case android.R.id.home:
			// app icon in action bar clicked; go home
			if(alternate)
				intentHome = new Intent(MessageList.this, AlternateSend.class);
			else
				intentHome = new Intent(MessageList.this, Channel.class);
			intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intentHome.putExtra("channelName", channel);
			intentHome.putExtra("category", cat);
			intentHome.putExtra("english",english );
			startActivity(intentHome);
			return true;

		default:
			return super.onMenuItemSelected(featureId, item);
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent viewMessage = new Intent(this, WebViewT.class);
		viewMessage.putExtra("website", messages.get(position).getLink()
				.toExternalForm());
		viewMessage.putExtra("channelName", channel);
		viewMessage.putExtra("category", cat);
		viewMessage.putExtra("english",english );
		viewMessage.putExtra("alternate", alternate);

		this.startActivity(viewMessage);
	}

	private void loadFeed2(ParserType type) {
		menuItems = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < counter; i++) {
			try {
				Log.i("AndroidNews", "ParserType=" + type.name());
				FeedParser parser = FeedParserFactory.getParser(type, urlArray[i]);
				long start = System.currentTimeMillis();
				messages = parser.parse();
				long duration = System.currentTimeMillis() - start;
				Log.i("AndroidNews", "Parser duration=" + duration);
				String xml = writeXml();
				Log.i("AndroidNews", xml);
				/*
				 * List<String> titles = new ArrayList<String>(messages.size()); for
				 * (Message msg : messages){ titles.add(msg.getTitle()); }
				 */
				
				for (Message msg : messages) {
					// creating new HashMap
					HashMap<String, String> map = new HashMap<String, String>();
					
					map.put(TAG_IMAGE, msg.getImageLink());
					map.put(TAG_TITLE, msg.getTitle());
					if (!channel.equals("Nawai Waqt Urdu"))
						try {
							map.put(TAG_DESCRIPTION, msg.getDescription());
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							map.put(TAG_DESCRIPTION, "");
						}
					else
						map.put(TAG_DESCRIPTION, "");
					try {
						map.put(TAG_DATE, msg.getDate());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						map.put(TAG_DATE, "Date Not Found");
					}
	
					menuItems.add(map);
				}
				/*
				 * SimpleAdapter adapter = new SimpleAdapter( MessageList.this,
				 * menuItems, R.layout.row_cat1, new String[] {TAG_TITLE,
				 * TAG_DESCRIPTION}, new int[] { R.id.title,R.id.description1});
				 */
				/*
				 * ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				 * R.layout.row_cat1,titles); this.setListAdapter(adapter);
				 * this.setListAdapter(adapter);
				 */
				// updateListUI(menuItems);
			} catch (Throwable t) {
				Log.e("AndroidNews", t.getMessage(), t);
			}
		}
			
	}	
	
	private void loadFeed(ParserType type) {
		try {
			Log.i("AndroidNews", "ParserType=" + type.name());
			FeedParser parser = FeedParserFactory.getParser(type, url);
			long start = System.currentTimeMillis();
			messages = parser.parse();
			long duration = System.currentTimeMillis() - start;
			Log.i("AndroidNews", "Parser duration=" + duration);
			String xml = writeXml();
			Log.i("AndroidNews", xml);
			/*
			 * List<String> titles = new ArrayList<String>(messages.size()); for
			 * (Message msg : messages){ titles.add(msg.getTitle()); }
			 */
			menuItems = new ArrayList<HashMap<String, String>>();
			for (Message msg : messages) {
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				map.put(TAG_IMAGE, msg.getImageLink());
				map.put(TAG_TITLE, msg.getTitle());
				if (!channel.equals("Nawai Waqt Urdu"))
					try {
						map.put(TAG_DESCRIPTION, msg.getDescription());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						map.put(TAG_DESCRIPTION, "");
					}
				else
					map.put(TAG_DESCRIPTION, "");
				try {
					map.put(TAG_DATE, msg.getDate());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					map.put(TAG_DATE, "Date Not Found");
				}

				menuItems.add(map);
			}
			/*
			 * SimpleAdapter adapter = new SimpleAdapter( MessageList.this,
			 * menuItems, R.layout.row_cat1, new String[] {TAG_TITLE,
			 * TAG_DESCRIPTION}, new int[] { R.id.title,R.id.description1});
			 */
			/*
			 * ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
			 * R.layout.row_cat1,titles); this.setListAdapter(adapter);
			 * this.setListAdapter(adapter);
			 */
			// updateListUI(menuItems);
		} catch (Throwable t) {
			Log.e("AndroidNews", t.getMessage(), t);
		}
	}

	private void updateListUI() {
		// TODO Auto-generated method stub
		/*
		 * SimpleAdapter adapter = new SimpleAdapter( MessageList.this,
		 * menuItems, R.layout.row_cat1, new String[] {TAG_TITLE,
		 * TAG_DESCRIPTION,TAG_DATE}, new int[] {
		 * R.id.title,R.id.description1,R.id.date});
		 */

		com.elegantsoft.pakistannews.CutomBaseAdapter adapter = new CutomBaseAdapter(
				MessageList.this, menuItems);
		Log.d("UI Update", " "+urlArray);
		try {
			if (!anyone && channel.equals("Selected Channels") || urlArray.length <= 0) {
				Toast.makeText(getApplicationContext(), "Please Select channels from setting", 
						Toast.LENGTH_LONG).show();
			}
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/*
		 * ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		 * R.layout.row_cat1,titles); this.setListAdapter(adapter);
		 */
		this.setListAdapter(adapter);
	}

	private String writeXml() {
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try {
			serializer.setOutput(writer);
			serializer.startDocument("UTF-8", true);
			serializer.startTag("", "messages");
			serializer.attribute("", "number", String.valueOf(messages.size()));
			for (Message msg : messages) {
				serializer.startTag("", "message");
				try {
					serializer.attribute("", "date", msg.getDate());
				} catch (NullPointerException e) {
					// TODO: handle exception
					// serializer.attribute("", "date",
					// "Information Not found");
				}
				
				serializer.startTag("", "title");
				try {
					serializer.text(msg.getTitle());
				} catch (NullPointerException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				serializer.endTag("", "title");
				serializer.startTag("", "url");
				serializer.text(msg.getLink().toExternalForm());
				serializer.endTag("", "url");
				serializer.startTag("", "body");
				try {
					serializer.text(msg.getDescription());
				} catch (NullPointerException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				serializer.endTag("", "body");
				serializer.endTag("", "message");
			}
			serializer.endTag("", "messages");
			serializer.endDocument();
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);

		}
	}

	class LoadFeed extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			pDialog = new ProgressDialog(MessageList.this);
			pDialog.setMessage("Loading Feeds...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub

			// url = "http://tribune.com.pk/pakistan/feed/";
			if(channel.equals("Selected Channels")){
				loadFeed2(ParserType.XML_PULL);
			}
			else
				loadFeed(ParserType.XML_PULL);
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			updateListUI();
			pDialog.dismiss();

		}
	}
	
	 @Override
	  public void onStart() {
	    super.onStart();
	  
	   // EasyTracker.getInstance().activityStart(this); // Google Analytics.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    
	   // EasyTracker.getInstance().activityStop(this); // Google Analytics.
	  }

/*	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
			// TODO Auto-generated method stub
		Intent intentHome;
			switch (item.getItemId()) {
			
			case android.R.id.home:
				// app icon in action bar clicked; go home
				if(alternate)
					intentHome = new Intent(MessageList.this, AlternateSend.class);
				else
					intentHome = new Intent(MessageList.this, Channel.class);
				intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intentHome.putExtra("channelName", channel);
				intentHome.putExtra("category", cat);
				startActivity(intentHome);
				return true;

			default:
				return super.onOptionsItemSelected(item);
			}
		
	}*/
	
/*	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Intent intentHome;
		switch (item.getItemId()) {
		
		case android.R.id.home:
			// app icon in action bar clicked; go home
			if(alternate)
				intentHome = new Intent(MessageList.this, AlternateSend.class);
			else
				intentHome = new Intent(MessageList.this, Channel.class);
			intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intentHome.putExtra("channelName", channel);
			intentHome.putExtra("category", cat);
			startActivity(intentHome);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}
	@Override
	public boolean onMenuItemSelected(int featureId,com.actionbarsherlock.view.MenuItem item) {
		// TODO Auto-generated method stub
		Intent intentHome;
		switch (item.getItemId()) {
		
		case android.R.id.home:
			// app icon in action bar clicked; go home
			if(alternate)
				intentHome = new Intent(MessageList.this, AlternateSend.class);
			else
				intentHome = new Intent(MessageList.this, Channel.class);
			intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intentHome.putExtra("channelName", channel);
			intentHome.putExtra("category", cat);
			startActivity(intentHome);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}*/
}